//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ArithmeticASOPathPricer.h"
using namespace Cephei::QL::Pricingengines::Asian;
#include <gen/QL/Methods/Montecarlo/Path.h>
using namespace Cephei::QL::Methods::Montecarlo;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::CArithmeticASOPathPricer (QL::Option::TypeEnum type, Double discount, Microsoft::FSharp::Core::FSharpOption<Double>^ runningSum, Microsoft::FSharp::Core::FSharpOption<UInt64>^ pastFixings) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phArithmeticASOPathPricer = NULL;
#endif
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::DiscountFactor _discount = (QuantLib::DiscountFactor)ValueHelper::Convert (discount); //d
        QuantLib::Real _runningSum = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (runningSum) ? (QuantLib::Real)ValueHelper::Convert (runningSum->Value) : 0.0); //4
        QuantLib::Size _pastFixings = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (pastFixings) ? (QuantLib::Size)ValueHelper::Convert (pastFixings->Value) : 0); //4
        _ppArithmeticASOPathPricer = new boost::shared_ptr<QuantLib::ArithmeticASOPathPricer> (new QuantLib::ArithmeticASOPathPricer ( _type,  _discount,  _runningSum,  _pastFixings ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::CArithmeticASOPathPricer (boost::shared_ptr<QuantLib::ArithmeticASOPathPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phArithmeticASOPathPricer = NULL;
#endif
	_ppArithmeticASOPathPricer = &childNative;
    
}
Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::CArithmeticASOPathPricer (QuantLib::ArithmeticASOPathPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phArithmeticASOPathPricer = NULL;
#endif
	_ppArithmeticASOPathPricer = new boost::shared_ptr<QuantLib::ArithmeticASOPathPricer> (&childNative);
    
    _ArithmeticASOPathPricerOwner = owner;
    
}

Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::CArithmeticASOPathPricer (CArithmeticASOPathPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phArithmeticASOPathPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppArithmeticASOPathPricer = new boost::shared_ptr<QuantLib::ArithmeticASOPathPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::CArithmeticASOPathPricer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phArithmeticASOPathPricer = NULL;
#endif
	if (!t->IsSubclassOf(CArithmeticASOPathPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::CArithmeticASOPathPricer (QuantLib::Handle<QuantLib::ArithmeticASOPathPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phArithmeticASOPathPricer = &childNative;
	_ppArithmeticASOPathPricer = &static_cast<boost::shared_ptr<QuantLib::ArithmeticASOPathPricer>>(childNative.currentLink());
    
    _ArithmeticASOPathPricerOwner = owner;
}
Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::CArithmeticASOPathPricer (QuantLib::Handle<QuantLib::ArithmeticASOPathPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phArithmeticASOPathPricer = &childNative;
	_ppArithmeticASOPathPricer = &static_cast<boost::shared_ptr<QuantLib::ArithmeticASOPathPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::CArithmeticASOPathPricer (QuantLib::ArithmeticASOPathPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phArithmeticASOPathPricer = NULL;
#endif
	_ppArithmeticASOPathPricer = new boost::shared_ptr<QuantLib::ArithmeticASOPathPricer> (new QuantLib::ArithmeticASOPathPricer (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::~CArithmeticASOPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppArithmeticASOPathPricer != NULL)
    {
	    delete _ppArithmeticASOPathPricer;
        _ppArithmeticASOPathPricer = NULL;
    }
}
Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::!CArithmeticASOPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppArithmeticASOPathPricer != NULL)
    {
	    delete _ppArithmeticASOPathPricer;
    }
}
QuantLib::ArithmeticASOPathPricer& Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::GetReference ()
{
    if (_ppArithmeticASOPathPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppArithmeticASOPathPricer;
}
boost::shared_ptr<QuantLib::ArithmeticASOPathPricer>& Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::GetShared ()
{
    if (_ppArithmeticASOPathPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppArithmeticASOPathPricer;
}
QuantLib::ArithmeticASOPathPricer* Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::GetPointer ()
{
    if (_ppArithmeticASOPathPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppArithmeticASOPathPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ArithmeticASOPathPricer>& Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::GetHandle ()
{
	if (_phArithmeticASOPathPricer == NULL)
	{
		_phArithmeticASOPathPricer = new Handle<QuantLib::ArithmeticASOPathPricer> (*_ppArithmeticASOPathPricer);
	}
	return *_phArithmeticASOPathPricer;
}
#endif
bool Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer::HasNative () 
{
	return (_ppArithmeticASOPathPricer != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Asian::IArithmeticASOPathPricer^ Cephei::QL::Pricingengines::Asian::CArithmeticASOPathPricer_Factory::Create (QL::Option::TypeEnum type, Double discount, Microsoft::FSharp::Core::FSharpOption<Double>^ runningSum, Microsoft::FSharp::Core::FSharpOption<UInt64>^ pastFixings)
{
    return REFNEW CArithmeticASOPathPricer ( type,  discount,  runningSum,  pastFixings);
}
